home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1999 Spring / macformat-077.iso / Shareware Plus / Development / Akua Sweets 131 / Akua Sweets Examples / Imaging / Image => Finder Icons < prev    next >
Encoding:
Text File  |  1999-03-04  |  4.4 KB  |  208 lines  |  [TEXT/ToyS]

  1. (*
  2. Create/Replace image icons for image files dropped on the script.
  3.  
  4. Applet(NoDialog)
  5. *)
  6. -- Properties
  7. property kasPrefName : "Image => Icons V1.0"
  8.  
  9. property kasImgBox : {0, 0, 32, 32} -- Box of image
  10.  
  11. -- Globals
  12. global gasInfoWind -- Info window
  13. global gasInfoPos -- Position of info window
  14. global gasDrawing -- Draw window
  15. global gasDrawPos
  16.  
  17. global gasFoldersToDo -- The folders left to process
  18. global gasShown -- Number gone!
  19. global gasChecked -- Number checked!
  20.  
  21.  
  22. on open fsObjs
  23.     -- Load prefs, show window
  24.     pfLoad()
  25.     
  26.     -- Set up prefix
  27.     set gasShown to 0
  28.     set gasChecked to 0
  29.     set gasShowWind to 0
  30.     
  31.     set gasInfoWind to display info titled kasPrefName ¬
  32.         located at gasInfoPos ¬
  33.         message "Scanning…"
  34.     
  35.     -- Do files
  36.     set gasFoldersToDo to {}
  37.     
  38.     repeat with fsObj in fsObjs
  39.         set myInfo to (basic info for fsObj)
  40.         
  41.         if (system type of myInfo is "fold") then
  42.             set gasFoldersToDo to gasFoldersToDo & {fsObj}
  43.         else
  44.             DoOne(fsObj)
  45.         end if
  46.     end repeat
  47.     
  48.     -- Do folders
  49.     repeat while gasFoldersToDo is not {}
  50.         -- Pop one off the end
  51.         set n to the number of items of gasFoldersToDo
  52.         set fsObj to item n of gasFoldersToDo
  53.         
  54.         if (n > 1) then
  55.             set gasFoldersToDo to items 1 through (n - 1) of gasFoldersToDo
  56.         else
  57.             set gasFoldersToDo to {}
  58.         end if
  59.         
  60.         display info gasInfoWind ¬
  61.             message ("Folders to go: " & n) ¬
  62.             at line 6 ¬
  63.             using color (15 * 32)
  64.         
  65.         -- Process it
  66.         GoDeep(fsObj)
  67.     end repeat
  68.     
  69.     display info gasInfoWind message "DONE!"
  70.     
  71.     pause for 2 with seconds timing -- Let screen wait...
  72.     
  73.     beep
  74.     
  75.     set gasInfoPos to screen location of ¬
  76.         (display info gasInfoWind with disposal)
  77.     
  78.     beep
  79.     
  80.     set gasDrawPos to screen location of ¬
  81.         (display info gasDrawing with disposal)
  82.     
  83.     beep
  84.     
  85.     pfSave() -- Save window location
  86. end open
  87.  
  88.  
  89. on DoOne(fsObj)
  90.     set imageType to the image type in fsObj
  91.     set fname to (catalog name of (basic info for fsObj))
  92.     
  93.     display info gasInfoWind ¬
  94.         message fname ¬
  95.         at line 2
  96.     
  97.     if (imageType is not "") then
  98.         set gasDrawing to ¬
  99.             display drawing titled ("Icon Creation") ¬
  100.                 located at gasDrawPos ¬
  101.                 starting with (the image from fsObj)
  102.         
  103.         display info gasInfoWind ¬
  104.             message imageType ¬
  105.             at line 3
  106.         -- Get picture
  107.         set imagePic to the image from fsObj
  108.         -- Get its bounds
  109.         set pBox to picture bounds of (the picture info for imagePic)
  110.         -- Center it
  111.         set drawInto to PlaceInCenter(pBox, kasImgBox)
  112.         -- Draw image
  113.         draw a picture into gasDrawing ¬
  114.             inside of drawInto ¬
  115.             using data imagePic
  116.         -- Free image
  117.         set imagePic to ¬
  118.             capture picture from gasDrawing ¬
  119.                 using depth 16 with pixel conversion
  120.         -- Redraw - some images don't draw correctly (QT Problem)
  121.         draw a picture into gasDrawing ¬
  122.             inside of drawInto ¬
  123.             using data imagePic
  124.         -- Get Picture / Assign Icon
  125.         set the icon of fsObj to imagePic
  126.     else
  127.         display info gasInfoWind ¬
  128.             message ¬
  129.             "Unknown" at line 3
  130.     end if
  131. end DoOne
  132.  
  133.  
  134. on GoDeep(foldObj)
  135.     display info gasInfoWind ¬
  136.         message "Path: " & (foldObj as string)
  137.     
  138.     set daddy to foldObj as string
  139.     
  140.     -- Do kinds that match
  141.     display info gasInfoWind ¬
  142.         message "Scanning files" at line 5
  143.     
  144.     set myItems to the entries in foldObj ¬
  145.         whose kinds are a file
  146.     
  147.     repeat with myItem in myItems
  148.         DoOne((daddy & myItem) as alias)
  149.     end repeat
  150.     
  151.     -- Do folders
  152.     display info gasInfoWind ¬
  153.         message "Scanning subfolders" at line 5
  154.     
  155.     set myItems to the entries in foldObj ¬
  156.         whose kinds are a folder
  157.     
  158.     repeat with myItem in myItems
  159.         set gasFoldersToDo to gasFoldersToDo & {(daddy & myItem) as alias}
  160.     end repeat
  161.     
  162.     -- Done
  163.     display info gasInfoWind ¬
  164.         message "…" at line 5
  165. end GoDeep
  166.  
  167.  
  168. on PlaceInCenter(src, dst)
  169.     set sW to (item 3 of src) - (item 1 of src)
  170.     set sH to (item 4 of src) - (item 2 of src)
  171.     set dW to (item 3 of dst) - (item 1 of dst)
  172.     set dH to (item 4 of dst) - (item 2 of dst)
  173.     
  174.     set r to dW / sW
  175.     set rH to dH / sH -- ratios
  176.     
  177.     if (rH < r) then set r to rH
  178.     
  179.     -- Round to a .25 ratio if > 1
  180.     if (r > 1) then ¬
  181.         set r to 0.25 * (round (r * 4))
  182.     
  183.     -- Scale the src
  184.     set sW to round (sW * r)
  185.     set sH to round (sH * r)
  186.     set mW to round ((dW - sW) / 2) + (item 1 of dst)
  187.     set mH to round ((dH - sH) / 2) + (item 2 of dst)
  188.     
  189.     -- Center it
  190.     return {mW, mH, mW + sW, mH + sH}
  191. end PlaceInCenter
  192.  
  193.  
  194. on pfLoad()
  195.     try
  196.         set ourPrefs to load preference named kasPrefName
  197.     on error
  198.         set ourPrefs to {pfWLoc:{20, 40}}
  199.     end try
  200.     
  201.     set gasWLoc to pfWLoc of ourPrefs
  202. end pfLoad
  203.  
  204.  
  205. on pfSave()
  206.     save preference {pfWLoc:gasWLoc} named kasPrefName
  207. end pfSave
  208.